package com.bigkoo.pickerviewdemo;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.dankegongyu.lib.wheelpicker.OptionsPickerView;

import java.util.List;

/**
 * 1列、2列、3列选项
 * @author WuPuquan
 * @version 1.0
 * @since 2017/11/3 11:09
 */

public class WheelOptionsPicker {

    private Context context;
    private TextView textView;
    private String title;
    private List<String> options;
    private List<List<String>>  secondOptions;
    private List<List<List<String>>>  thirdOptions;
    private int firstSelectedPosition;
    private int secondSelectedPosition;
    private int thirdSelectedPosition;
    private OptionsPickerView.OnOptionsSelectListener onOptionsSelectListener;

    private WheelOptionsPicker(Builder builder) {
        this.context = builder.context;
        this.textView = builder.textView;
        this.title = builder.title;
        this.options = builder.options;
        this.secondOptions = builder.secondOptions;
        this.thirdOptions = builder.thirdOptions;
        this.firstSelectedPosition = builder.firstSelectedPosition;
        this.secondSelectedPosition = builder.secondSelectedPosition;
        this.thirdSelectedPosition = builder.thirdSelectedPosition;
        this.onOptionsSelectListener = builder.onOptionsSelectListener;
    }

    public void show() {
        if (null == onOptionsSelectListener) {
            onOptionsSelectListener = new OptionsPickerView.OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int options2, int options3, View v) {
                    if (null != textView) {
                        textView.setText(options.get(options1));
                    }
                }
            };
        }
        OptionsPickerView pvOptions = new OptionsPickerView.Builder(context, onOptionsSelectListener)
                .setTitleSize(16)
                .setSubCalSize(14)//确定取消按钮大小
                .setContentTextSize(16)
                .setTitleText(TextUtils.isEmpty(title) ? "" : title)
                .setTitleColor(0xFF999999)
                .setCancelColor(0xFF999999)
                .setSubmitColor(0xFF3dbcc6)
                .setTextColorCenter(0xFF999999) //设置选中项文字颜色
                .setDividerColor(0xFFf0f0f0)//设置分割线的颜色
//                .setBackgroundColor(Color.WHITE)
//                .setLinkage(false)//default is true
//                .setCyclic(false, false, false)//循环与否
//                .setOutSideCancelable(false)//点击外部dismiss, default true
//                .setTitleBgColor(0xFF333333)//标题背景颜色 Night mode
//                .setBgColor(0xFF000000)//滚轮背景颜色 Night mode
//                .setLabels("省", "市", "区")//设置选择的三级单位
                .setItemHeight(150) //设置两横线之间的间隔倍数（范围：1.2 - 2.0倍 文字高度）
                .isDialog(true)//是否设置为对话框模式
//                .setOutSideCancelable(false)//点击屏幕中控件外部范围，是否可以取消显示，default is true*/
//                .setDividerType(WheelView.DividerType.WRAP)
                .setSelectOptions(firstSelectedPosition, secondSelectedPosition, thirdSelectedPosition)  //设置默认选中项
                .build();

        //noinspection unchecked
        pvOptions.setPicker(options, secondOptions, thirdOptions);//一级选择器
        pvOptions.show();
    }

    public static class Builder{

        private Context context;
        private TextView textView;
        private String title;
        private List<String> options;
        private List<List<String>>  secondOptions;
        private List<List<List<String>>>  thirdOptions;
        private int firstSelectedPosition;
        private int secondSelectedPosition;
        private int thirdSelectedPosition;
        private OptionsPickerView.OnOptionsSelectListener onOptionsSelectListener;

        public Builder(Context context) {
            this.context = context;
        }

        /**
         * 默认一列单选，如果textView不为空直接设置内容；
         * 如果有多列，显示规则可能不同，可以设置监听{@link #setOnOptionsSelectListener(OptionsPickerView.OnOptionsSelectListener)}
         * @param textView
         * @return
         */
        public Builder onTextView(TextView textView) {
            this.textView = textView;
            return this;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        /**
         * 默认一列单选，如果有两列三列需求请设置{@link #setSecondOptions} 和 {@link #setThirdOptions}
         * @param options
         * @return
         */
        public Builder setOptions(List<String> options) {
            this.options = options;
            return this;
        }

        public Builder setSecondOptions(List<List<String>> secondOptions) {
            this.secondOptions = secondOptions;
            return this;
        }

        public Builder setThirdOptions(List<List<List<String>>> thirdOptions) {
            this.thirdOptions = thirdOptions;
            return this;
        }

        public Builder setFirstSelectedPosition(int firstSelectedPosition) {
            this.firstSelectedPosition = firstSelectedPosition;
            return this;
        }

        public Builder setSecondSelectedPosition(int secondSelectedPosition) {
            this.secondSelectedPosition = secondSelectedPosition;
            return this;
        }

        public Builder setThirdSelectedPosition(int thirdSelectedPosition) {
            this.thirdSelectedPosition = thirdSelectedPosition;
            return this;
        }

        public Builder setOnOptionsSelectListener(OptionsPickerView.OnOptionsSelectListener onOptionsSelectListener) {
            this.onOptionsSelectListener = onOptionsSelectListener;
            return this;
        }

        public WheelOptionsPicker build() {
            return new WheelOptionsPicker(this);
        }

    }
}
